package config

import (
    "github.com/go-ini/ini"
    "time"
    "wwgin/global/variable"
)

type App struct {
    Debug          bool
    Logs           Logs
    Redis          map[string]RedisConf
    TrustedProxies []string //设置信任的代理,为空时表示信任全部,https://www.cnblogs.com/mayanan/p/15703234.html
}

type Logs struct {
    GinLog        bool   //是否开启gin的访问日志 - 如果使用nginx代理可以关闭gin日志提高性能
    GinLogName    string //设置 gin 框架的接口访问日志
    PrjLogName    string //项目运行时日志文件名，注意该名称不要与上一条重复 ,避免和 gin 框架的日志掺杂一起，造成混乱。
    TextFormat    string //记录日志的格式，参数选项：console、json ， console 表示一般的文本格式
    TimePrecision string //记录日志时，相关的时间精度，该参数选项：second  、 millisecond ， 分别表示 秒 和 毫秒 ,默认为毫秒级别
    MaxSize       int    //每个日志的最大尺寸(以MB为单位）， 超过该值，系统将会自动进行切割
    MaxBackups    int    //保留旧日志最大个数
    MaxAge        int    //保留旧日志最大天数
    Compress      bool   //是否压缩
}

type RedisConf struct {
    Host               string
    Port               string
    Auth               string
    MaxIdle            int
    MaxActive          int
    IdleTimeout        time.Duration
    IndexDb            int
    ConnFailRetryTimes int           //从连接池获取连接失败，最大重试次数
    ReConnectInterval  time.Duration //从连接池获取连接失败，每次重试之间间隔的秒数
}

var AppConf App

func init() {

    AppConf.Redis = map[string]RedisConf{} //初始化redis的配置

    switch variable.Env {
    case "online": //生产环境
        AppConf.Debug = false
        //信任代理设置
        AppConf.TrustedProxies = []string{}
        //日志的配置
        AppConf.Logs.GinLog = false
        AppConf.Logs.GinLogName = "/data/log/gin.log"
        AppConf.Logs.PrjLogName = "/data/log/log.log"
        AppConf.Logs.TextFormat = "console"
        AppConf.Logs.TimePrecision = "second"
        AppConf.Logs.MaxSize = 50
        AppConf.Logs.MaxBackups = 500
        AppConf.Logs.MaxAge = 30
        AppConf.Logs.Compress = false
        //redis的配置 - 支持多个
        AppConf.Redis["def"] = RedisConf{
            Host:               "127.0.0.1",
            Port:               "6379",
            Auth:               "",
            MaxIdle:            200,
            IdleTimeout:        200,
            IndexDb:            0,
            ConnFailRetryTimes: 3,
            ReConnectInterval:  1,
        }
        AppConf.Redis["local"] = RedisConf{
            Host:               "127.0.0.1",
            Port:               "6379",
            Auth:               "",
            MaxIdle:            200,
            IdleTimeout:        200,
            IndexDb:            0,
            ConnFailRetryTimes: 3,
            ReConnectInterval:  1,
        }

    default: //测试环境
        AppConf.Debug = true
        //信任代理设置
        AppConf.TrustedProxies = []string{}
        //日志的配置
        AppConf.Logs.GinLog = false
        AppConf.Logs.GinLogName = "/data/log/gin.log"
        AppConf.Logs.PrjLogName = "/data/log/log.log"
        AppConf.Logs.TextFormat = "console"
        AppConf.Logs.TimePrecision = "second"
        AppConf.Logs.MaxSize = 50
        AppConf.Logs.MaxBackups = 500
        AppConf.Logs.MaxAge = 30
        AppConf.Logs.Compress = false
        //redis的配置 - 支持多个
        AppConf.Redis["def"] = RedisConf{
            Host:               "127.0.0.1",
            Port:               "6379",
            Auth:               "",
            MaxIdle:            200,
            IdleTimeout:        200,
            IndexDb:            0,
            ConnFailRetryTimes: 3,
            ReConnectInterval:  1,
        }
        AppConf.Redis["local"] = RedisConf{
            Host:               "127.0.0.1",
            Port:               "6379",
            Auth:               "",
            MaxIdle:            200,
            IdleTimeout:        200,
            IndexDb:            0,
            ConnFailRetryTimes: 3,
            ReConnectInterval:  1,
        }

    }

    //添加检查配置文件是否存在,存在的话,配置已配置文件为准
    load, err := ini.Load(variable.ROOT_PATH + "/config/config.ini")
    if err == nil {
        app := load.Section("app")
        //调试开关
        AppConf.Debug = app.Key("Debug").MustBool(AppConf.Debug)
        //日志的配置
        AppConf.Logs.GinLog = app.Key("GinLog").MustBool(AppConf.Logs.GinLog)
        AppConf.Logs.GinLogName = app.Key("GinLog").MustString(AppConf.Logs.GinLogName)
        AppConf.Logs.PrjLogName = app.Key("PrjLogName").MustString(AppConf.Logs.PrjLogName)
        AppConf.Logs.TextFormat = app.Key("TextFormat").MustString(AppConf.Logs.TextFormat)
        AppConf.Logs.TimePrecision = app.Key("TimePrecision").MustString(AppConf.Logs.TimePrecision)
        AppConf.Logs.MaxSize = app.Key("MaxSize").MustInt(AppConf.Logs.MaxSize)
        AppConf.Logs.MaxBackups = app.Key("MaxBackups").MustInt(AppConf.Logs.MaxBackups)
        AppConf.Logs.MaxAge = app.Key("MaxAge").MustInt(AppConf.Logs.MaxAge)
        AppConf.Logs.Compress = app.Key("Compress").MustBool(AppConf.Logs.Compress)

        //redis的配置 - 支持多个
        redis := load.Section("redis")
        redis_host := redis.Key("Host").String()
        if redis_host != "" {
            AppConf.Redis["def"] = RedisConf{
                Host:               redis.Key("Host").MustString(AppConf.Redis["def"].Host),
                Port:               redis.Key("Port").MustString(AppConf.Redis["def"].Port),
                Auth:               redis.Key("Auth").MustString(AppConf.Redis["def"].Auth),
                MaxIdle:            redis.Key("MaxIdle").MustInt(AppConf.Redis["def"].MaxIdle),
                MaxActive:          redis.Key("MaxActive").MustInt(AppConf.Redis["def"].MaxActive),
                IdleTimeout:        redis.Key("IdleTimeout").MustDuration(AppConf.Redis["def"].IdleTimeout),
                IndexDb:            redis.Key("IndexDb").MustInt(AppConf.Redis["def"].IndexDb),
                ConnFailRetryTimes: redis.Key("ConnFailRetryTimes").MustInt(AppConf.Redis["def"].ConnFailRetryTimes),
                ReConnectInterval:  redis.Key("ReConnectInterval").MustDuration(AppConf.Redis["def"].ReConnectInterval),
            }
        } else {
            delete(AppConf.Redis, "def")
        }
        //添加更多的redis配置 ...

    }
}
